<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <p>{{count}}</p>
      <button @click="increment({amount:5})">+</button>
      <button @click="decrement({amount:5})">-</button>
    </div>
    <script src="vue.js"></script>
    <script src="my-vuex.js"></script>
    <script>
      Vue.use(Vuex);
      const INCREMENT = 'increment';
      const DECREMENT = 'decrement';
      const store = new Vuex.Store({
        state:{count:0},//定义仓库的初始状态{count:0}
        mutations:{
          [INCREMENT](state,payload){
            state.count+=payload.amount;
          },
          [DECREMENT](state,payload){
            state.count-=payload.amount;
          }
        }
      });
      var vm = new Vue({
        el:'#app',
        store,
        computed:{
          count(){
            return this.$store.state.count;
          }
        },
        methods:{
          increment(payload){
            this.$store.commit(INCREMENT,payload);
          },
          //把仓库中的mutations映射为Vue实例的方法
          ...Vuex.mapMutations([DECREMENT])
        }
      });
    </script>
  </body>
</html>
